package com.elm.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.elm.entity.DeliveryAddress;
import com.elm.service.DeliveryAddressService;
import com.elm.service.impl.DeliveryAddressServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class DeliveryAddressController implements BaseController{
    private final DeliveryAddressService deliveryAddressService;
    public DeliveryAddressController() {
        deliveryAddressService = new DeliveryAddressServiceImpl();
    }
    @Override
    public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String action = req.getPathInfo().split("/")[2];
        switch (action) {
            case "saveDeliveryAddress":
                doSaveDeliveryAddress(req, resp);
                break;
            case "updateDeliveryAddress":
                doUpdateDeliveryAddress(req, resp);
                break;
            case "removeDeliveryAddress":
                doDeleteDeliveryAddress(req, resp);
                break;
            case "getDeliveryAddressById":
                doGetDeliveryAddress(req, resp);
                break;
            case "listDeliveryAddressByUserId":
                doGetListDeliveryAddressByUserId(req, resp);
                break;
        }
    }

    /**
     * 保存地址
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doSaveDeliveryAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setAddress(req.getParameter("address"));
        deliveryAddress.setUserId(req.getParameter("userId"));
        deliveryAddress.setContactSex(Integer.valueOf(req.getParameter("contactSex")));
        deliveryAddress.setContactTel(req.getParameter("contactTel"));
        deliveryAddress.setContactName(req.getParameter("contactName"));

        int result = deliveryAddressService.saveAddress(deliveryAddress);
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(result));
    }

    /**
     * 修改地址
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doUpdateDeliveryAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setAddress(req.getParameter("address"));
        deliveryAddress.setDaId(Integer.valueOf(req.getParameter("daId")));
        deliveryAddress.setContactSex(Integer.valueOf(req.getParameter("contactSex")));
        deliveryAddress.setContactTel(req.getParameter("contactTel"));
        deliveryAddress.setUserId(req.getParameter("userId"));
        deliveryAddress.setContactName(req.getParameter("contactName"));

        int result = deliveryAddressService.updateAddress(deliveryAddress);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(result));
    }

    /**
     * 删除地址
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doDeleteDeliveryAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int daId = Integer.parseInt(req.getParameter("daId"));

        int result = deliveryAddressService.deleteAddress(daId);
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(result));
    }

    /**
     * 根据Id查询地址
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doGetDeliveryAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int daId = Integer.parseInt(req.getParameter("daId"));
        DeliveryAddress deliveryAddress = deliveryAddressService.getAddressById(daId);
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(deliveryAddress));
    }

    /**
     * 获取所有的地址信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void doGetListDeliveryAddressByUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        List<DeliveryAddress> addresses = deliveryAddressService.getAddressList(userId);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(new Gson().toJson(addresses));
    }
}

